package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> setmealPage = setmealMapper.page(dto);
        long total = setmealPage.getTotal();
        List<SetmealVO> records = setmealPage.getResult();
        return new PageResult(total, records);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        //起售套餐是，判断套餐内是否有停售菜品，有停售菜品提示“套餐内包含未起售菜品，无法起售”
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getDishBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach( dish -> {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.updateStatus(setmeal);
    }

    @Override
    @Transactional
    public void insert(SetmealVO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) { // 88起售，89停售，90停售
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getSetmealById(id);
            if (StatusConstant.ENABLE == setmeal.getStatus()) {
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        try {
            //删除套餐表中的数据
            setmealMapper.deleteByIds(ids);
            //删除套餐菜关系表中的数据
            setmealDishMapper.deleteByIds(ids);
        } catch (Exception e) {
            throw new DeletionNotAllowedException(MessageConstant.NOT_SELECTED_UNABLE_TO_DELETE);
        }
    }

    @Override
    public void update(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.updateSetmeal(setmeal);

        setmealDishMapper.deleteBySetmealId(dto.getId());
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.stream().forEach(
                    (setmealDish -> {
                        setmealDish.setSetmealId(dto.getId());
                    })
            );
            setmealDishMapper.insertSetmealDishs(setmealDishes);
        }
        

    }

    @Override
    public List<Setmeal> getSetmealByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.getSetmealByCategoryId(categoryId);
        return setmeals;
    }

    @Override
    public List<DishItemVO> getDishBySetmealId(Long id) {
        List<DishItemVO> setmealDish = setmealDishMapper.getDishBySetmealId(id);
        return setmealDish;
    }
}














